package org.jypj.dev.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.jypj.dev.constant.URLConstant;
import org.jypj.dev.model.StationWeather;
import org.jypj.dev.model.TrainStation;

import java.util.*;

/**
 * Created by chenyuyuyu on 2017/5/30.
 */
public class WeatherUtil {

    private Map<String, JSONArray> stations = new HashMap<>();//站点集合 key站点名称


    public static void main(String[] args) {
        WeatherUtil weatherUtil = new WeatherUtil();
        TrainStation trainStation = new TrainStation();
        trainStation.setName("黄石");
        trainStation.setLongitude("115.05068316392397");
        trainStation.setLatitude("30.21612712771406");
        TrainStation trainStation2 = new TrainStation();
        trainStation2.setName("黄石");
        trainStation2.setLongitude("115.05068316392397");
        trainStation2.setLatitude("30.21612712771406");
        weatherUtil.getRoadWeatherByTrainStation(trainStation);
        weatherUtil.getRoadWeatherByTrainStation(trainStation2);

    }

    /**
     * 根据站点的经纬度得到交路天气对象
     *
     * @param trainStation
     * @return
     */
    public List<StationWeather> getRoadWeatherByTrainStation(TrainStation trainStation) {
        List<StationWeather> stationWeathers = new ArrayList<>();
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + URLConstant.MOJI_WEATHER_APPCODE);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<>();
        Map<String, String> bodys = new HashMap<>();
        bodys.put("lat", trainStation.getLatitude());
        bodys.put("lon", trainStation.getLongitude());
        bodys.put("token", "1b89050d9f64191d494c806f78e8ea36");
        try {
            JSONArray hourly = stations.get(trainStation.getName());
            if (hourly == null) {
                HttpResponse response = HttpUtil.doPost(URLConstant.MOJI_WEATHER_HOST, URLConstant.MOJI_WEATHER_PATH, method, headers, querys, bodys);
                String s = EntityUtils.toString(response.getEntity());//得到天气数据
                JSONObject jsonObject = JSONObject.parseObject(s);
                JSONObject data = jsonObject.getJSONObject("data");
                hourly = data.getJSONArray("hourly");
                stations.put(trainStation.getName(), hourly);
            }
            for (int i = 0; i < hourly.size() - 1; i++) {
                String todayString = DateUtils.getTodayString("yyyy-MM-dd HH:mm:ss");
                JSONObject o = (JSONObject) hourly.get(i);
                StationWeather stationWeather = new StationWeather();
                stationWeather.setId(StringUtil.getLowUUID());
                stationWeather.setStationName(trainStation.getName());
                stationWeather.setTemp(o.getString("temp"));
                stationWeather.setWeatherCondition(o.getString("condition"));
                stationWeather.setWindSpeed(o.getIntValue("windSpeed"));
                stationWeather.setWeatherDate(o.getString("date"));
                stationWeather.setWeatherHour(o.getIntValue("hour"));
                stationWeather.setWindType(getWindTypeBySpeed(o.getIntValue("windSpeed")));
                stationWeather.setIsAlert(o.getIntValue("windSpeed") > 38 ? 1 : 0);//如果风速大于38就需要预警
                stationWeather.setCreateTime(todayString);
                stationWeather.setQueryTime(o.getString("date") + " " + o.getIntValue("hour"));
                stationWeathers.add(stationWeather);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stationWeathers;
    }

    public String getWindTypeBySpeed(int windSpeed) {
        String windType = "";
        if (0 <= windSpeed && windSpeed < 1) {
            windType = "无风";
        }
        if (1 <= windSpeed && windSpeed < 12) {
            windType = "轻风";
        }
        if (12 <= windSpeed && windSpeed < 38) {
            windType = "微风";
        }
        if (38 <= windSpeed && windSpeed < 61) {
            windType = "疾风";
        }
        if (windSpeed >= 61) {
            windType = "狂风";
        }
        return windType;
    }

}
